package com.woniuxy.woniuticket.moviemodule.service.impl;

import com.woniuxy.woniuticket.moviemodule.dao.CinemaSeatsDAO;
import com.woniuxy.woniuticket.moviemodule.service.CinemaSeatsService;
import com.woniuxy.woniuticket.pojo.CinemaSeats;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author Utopia
 * @title: CinemaSeatsServiceImpl
 * @projectName woniuticket
 * @description: TODO
 * @date 2019/5/30 17:51
 */

@Service
public class CinemaSeatsServiceImpl implements CinemaSeatsService {

    @Resource
    private CinemaSeatsDAO cinemaSeatsDAO;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public boolean seatCheck(List<CinemaSeats> list) {
        //设置序列化器
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(CinemaSeats.class));
        boolean flag = true;
        for (CinemaSeats cinemaSeats: list) {
            //非空检查
            if (!nullCheck(cinemaSeats)) {
                return false;
            }
            //查询选中座位信息是否已经被占用
            cinemaSeats = (CinemaSeats) redisTemplate.opsForValue().get(cinemaSeats.getScheduleId() + ":" + cinemaSeats.getSeatcolumn() + "," + cinemaSeats.getSeatrow());
            if (cinemaSeats != null) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    @Override
    public List<CinemaSeats> selectSeatsBySid(CinemaSeats cinemaSeats) {
        if (cinemaSeats == null || cinemaSeats.getHallId() == null || cinemaSeats.getScheduleId() == null) {
            return null;
        }
        //先查询数据库已购买座位的数据
        List<CinemaSeats> list = cinemaSeatsDAO.selectSeatsBySid(cinemaSeats);
        //设置Json序列化器
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(CinemaSeats.class));
        //redis模糊查询key集合
        Set<String> keys = redisTemplate.keys(cinemaSeats.getScheduleId() + "*");
        for (String str : keys) {
            //将内容查询出来放入list并返回
            CinemaSeats seat = (CinemaSeats) redisTemplate.opsForValue().get(str);
            if (seat != null) {
                list.add(seat);
            }
        }
        return list;
    }

    @Override
    public int insertCinemaSeatsToRedis(List<CinemaSeats> lists) {
        for (CinemaSeats cinemaSeats: lists) {
            boolean flag = nullCheck(cinemaSeats);
            if (!flag) {
                return 0;
            }
            String key = cinemaSeats.getScheduleId() + ":" + cinemaSeats.getSeatcolumn() + "," + cinemaSeats.getSeatrow();
            redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(CinemaSeats.class));
            redisTemplate.opsForValue().set(key, cinemaSeats);
            //设置未付款订单座位信息过期时间为30分钟
            redisTemplate.expire(key, 30, TimeUnit.MINUTES);

        }
        return 1;
    }

    @Override
    public int insertCinemaSeatsToMysql(CinemaSeats cinemaSeats) {
        boolean flag = nullCheck(cinemaSeats);
        if (!flag) {
            return 0;
        }
        String key = cinemaSeats.getScheduleId() + ":" + cinemaSeats.getSeatcolumn() + "," + cinemaSeats.getSeatrow();
        redisTemplate.delete(key);
        return 0;
        //return cinemaSeatsDAO.insertSelective(cinemaSeats);
    }

    @Override
    public int updateCinemaSeats(CinemaSeats cinemaSeats) {
        return cinemaSeatsDAO.updateByPrimaryKeySelective(cinemaSeats);
    }

    //非空检查
    public boolean nullCheck(CinemaSeats cinemaSeats) {
        if (cinemaSeats == null || cinemaSeats.getScheduleId() == null || cinemaSeats.getSeatcolumn() == null || cinemaSeats.getSeatrow() == null) {
            return false;
        }
        return true;
    }

}
